﻿using Newtonsoft.Json;
using WeChatParking.Core.Payment.WeixinPay;
using WeChatParking.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeChatParking.Core.Payment.Tonglian
{
    public class TonglianNew
    {
        /// <summary>
        /// 统一支付接口
        /// </summary>
        /// <param name="trxamt">交易金额</param>
        /// <param name="reqsn">订单号</param>
        /// <param name="paytype">交易方式：W02:微信JS支付、A02:支付宝JS支付、Q02:QQ钱包JS支付</param>
        /// <param name="body">订单商品名称，空是商户名作为商品名称</param>
        /// <param name="remark">备注</param>
        /// <param name="acct">支付平台标识openid</param>
        /// <param name="authcode"></param>
        /// <param name="notify_url">交易结果通知地址</param>
        /// <param name="limit_pay">支付限制--不能使用信用卡支付</param>
        /// <returns></returns>
        public Dictionary<String, String> pay(long trxamt, String reqsn, String paytype, String body, String remark, String acct, String authcode, String notify_url, out string request, String limit_pay = "no_credit")
        {
            Dictionary<String, String> paramDic = buildBasicParam();
            paramDic.Add("trxamt", trxamt.ToString());
            paramDic.Add("reqsn", reqsn);
            paramDic.Add("paytype", paytype);
            paramDic.Add("body", body);
            paramDic.Add("remark", remark);
            paramDic.Add("acct", acct);
            paramDic.Add("authcode", authcode);
            paramDic.Add("notify_url", notify_url);
            paramDic.Add("limit_pay", limit_pay);
            paramDic.Add("sign", AppUtil.signParam(paramDic, WxpayConfig.WxPayKey));
            request = JsonConvert.SerializeObject(paramDic);
            return doRequest(paramDic, "/pay");
        }
        /// <summary>
        /// 交易撤销
        /// </summary>
        /// <param name="trxamt"></param>
        /// <param name="reqsn"></param>
        /// <param name="oldtrxid"></param>
        /// <param name="oldreqsn"></param>
        /// <returns></returns>
        public Dictionary<String, String> cancel(long trxamt, String reqsn, String oldtrxid, String oldreqsn)
        {
            Dictionary<String, String> paramDic = buildBasicParam();
            paramDic.Add("trxamt", trxamt.ToString());
            paramDic.Add("reqsn", reqsn);
            paramDic.Add("oldtrxid", oldtrxid);
            paramDic.Add("oldreqsn", oldreqsn);
            paramDic.Add("sign", AppUtil.signParam(paramDic, WxpayConfig.WxPayKey));
            return doRequest(paramDic, "/cancel");
        }
        /// <summary>
        /// 交易退款
        /// </summary>
        /// <param name="trxamt"></param>
        /// <param name="reqsn"></param>
        /// <param name="oldtrxid"></param>
        /// <param name="oldreqsn"></param>
        /// <returns></returns>
        public Dictionary<String, String> refund(long trxamt, String reqsn, String oldtrxid, String oldreqsn)
        {
            Dictionary<String, String> paramDic = buildBasicParam();
            paramDic.Add("trxamt", trxamt.ToString());
            paramDic.Add("reqsn", reqsn);
            paramDic.Add("oldtrxid", oldtrxid);
            paramDic.Add("oldreqsn", oldreqsn);
            paramDic.Add("sign", AppUtil.signParam(paramDic, WxpayConfig.WxPayKey));
            return doRequest(paramDic, "/refund");
        }
        /// <summary>
        /// 交易查询
        /// </summary>
        /// <param name="reqsn"></param>
        /// <param name="trxid"></param>
        /// <returns></returns>
        public Dictionary<String, String> query(String reqsn, String trxid)
        {
            Dictionary<String, String> paramDic = buildBasicParam();
            paramDic.Add("reqsn", reqsn);
            paramDic.Add("trxid", trxid);
            paramDic.Add("sign", AppUtil.signParam(paramDic, WxpayConfig.WxPayKey));
            return doRequest(paramDic, "/query");
        }

        private Dictionary<String, String> buildBasicParam()
        {
            Dictionary<String, String> paramDic = new Dictionary<String, String>();
            paramDic.Add("cusid", WxpayConfig.MchID);
            paramDic.Add("appid", WxpayConfig.TonglianAppId);
            paramDic.Add("version", "11");
            paramDic.Add("randomstr", DateTime.Now.ToFileTime().ToString());
            return paramDic;

        }

        private Dictionary<String, String> doRequest(Dictionary<String, String> param, String url)
        {
            String rsp = HttpUtil.CreatePostHttpResponse(WxpayConfig.UnifiedOrderUrl + url, param, Encoding.UTF8);
            Dictionary<String, String> rspDic = (Dictionary<String, String>)JsonConvert.DeserializeObject(rsp, typeof(Dictionary<String, String>));
            if ("SUCCESS".Equals(rspDic["retcode"]))//验签
            {
                String signRsp = rspDic["sign"];
                rspDic.Remove("sign");
                String sign = AppUtil.signParam(rspDic, WxpayConfig.WxPayKey);
                if (signRsp.Equals(sign))
                {
                    return rspDic;
                }
                else
                    throw new Exception("验签失败");

            }
            else
            {
                throw new Exception(rspDic["retmsg"]);
            }
        }

    }
}
